<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
	class Article extends CI_Controller {
		public $table_name = "article";
        public $pagename = "article";
        public $validation_config = array(
            array(
                'field' => 'title',
                'label' => '标题',
                'rules' => 'required'
            ),
            array(
                'field' => 'catalog_id',
                'label' => '分类',
                'rules' => 'required'
            ),
            array(
                'field' => 'desc',
                'label' => '描述',
                'rules' => 'required'
            ),
            array(
                'field' => 'content',
                'label' => '内容',
                'rules' => 'required'
            ),
            array(
                'field' => 'img',
                'label' => '标题',
                'rules' => 'required'
            ),
        );
        public function __construct(){
            parent::__construct();
            if(!$this->session->userdata('is_login')){
                redirect("/admin/auth/login");
            }
            $this->load->model("admin/Article_model");
        }
        public function index($page=1){
            $page_size = 5;
            $total = $this->Article_model->count_All();
            $articles = $this->Article_model->get_by_page($page,$page_size);
            $data = array(
                'articles' => $articles,
                );
                $this->load->library('pagination');
                $config['base_url'] = site_url('admin/article/index/');
                $config['total_rows'] = $total;
                $config['per_page'] = $page_size;
                $config['use_page_numbers'] = TRUE;
                $this->pagination->initialize($config);
                $this->load->view("admin/Article_index",$data);
        }
        public function view_once($id){
            $query = $this->db->query( "update $this->table_name set `view` = view + 1 where `id` = $id");
        }
            public function add(){
            $this->load->library('form_validation');
            $this->form_validation->set_rules($this->validation_config);
            if ($this->form_validation->run() == FALSE){
                $navs = $this->Article_model->add();
                $article = new stdCLASS();
                $article->title = "";
                $article->catalog_id = 0;
                $article->desc = "";
                $article->content = "";
                $article->img = "";
                    $data = array(
                        "navs" => $navs,
                        "title" => "添加",
                        "article" => $article,
                    );
                $this->load->view("admin/Article_form",$data);
            }else{
                $data = $this->input->post();
                $this->Article_model->Add_save($data);
                $this->session->set_userdata('is_login', true);
                redirect("/admin/article/index");
            }
        }
        public function edit($id){
            $this->load->library('form_validation');
           $this->form_validation->set_rules($this->validation_config);
            if ($this->form_validation->run() == FALSE){
                $navs = $this->Article_model->add();
                $article = $this->Article_model->get_One($id);
                    $data = array(
                        "navs" => $navs,
                        "title" => "修改",
                        "article" => $article,
                    );
                $this->load->view("admin/Article_form",$data);
            }else{
                $data = $this->input->post();
                $this->Article_model->Edit_save($id,$data);
                $this->session->set_userdata('is_login', true);
                redirect("/admin/article/index");
            }
        }
        public function del($id){
            // $id = $this->uri->segment(4);
          $this->Article_model->Delete($id);
            redirect("/admin/article/index");
        }
        public function recommend($id,$value){
            $this->Article_model->edit_Recommend($id,$value);
            redirect("/admin/article/index");
        }
	}
?>